// 评论模块
const router =  require('express').Router()
const query = require('../mysql/index');

/**
 * 获取留言别表
 */
router.get("/getAll",async(req,res)=>{
    try {
        let sql = "select *,(select username from user_table where id = message.user_id) as username from message where article_id = ? order by id desc";
        let data = await query(sql,[ req.query.article_id]);
        res.json({
            code:200,
            data
        })
    } catch (error) {
        res.json({
            code:400,
            
        })
    }
})

/**
 * 评论
 */
router.post('/add',async(req,res)=>{
    try {
        let sql = "insert into message value(null,?,?,?,?)";
        await query(sql,[
            req.body.article_id,
            req.body.user_id,
            req.body.content,
            req.body.create_time,
        ]);
        res.json({code:200})
    } catch (error) {
        res.json({code:400})
    }    
})



/**
 * 删除自己的评论
 */
module.exports = router;